package com.alliky.service.impl;

import com.alliky.enums.Sex;
import com.alliky.mapper.UsersMapper;
import com.alliky.pojo.Users;
import com.alliky.pojo.bo.UsersBO;
import com.alliky.service.IUserService;
import com.alliky.utils.DateTimeUtil;
import com.alliky.utils.MD5Util;
import com.alliky.utils.PropertiesUtil;
import com.alliky.common.ServerResponse;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    public UsersMapper usersMapper;
    //生成自增长的唯一ID
    private Sid sid;

    public static final String USER_FACE = PropertiesUtil.getProperty("user.portrait", "");

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {

        Example userExample = new Example(Users.class);
        Example.Criteria userCriteria = userExample.createCriteria();

        userCriteria.andEqualTo("username", username);

        Users result = usersMapper.selectOneByExample(userExample);

        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ServerResponse<Users> createUser(UsersBO usersBO) {

        String userId = sid.nextShort();

        Users user = new Users();
        user.setId(userId);
        user.setUsername(usersBO.getUsername());
        //用户密码
        user.setPassword(MD5Util.MD5EncodeUtf8(usersBO.getPassword()));
        //默认用户昵称同用户名
        user.setNickname(usersBO.getUsername());
        //默认头像
        user.setFace(USER_FACE);
        //默认生日
        user.setBirthday(DateTimeUtil.strToDate("1970-01-01", DateTimeUtil.YTD_FORMAT));
//      //默认性别 保密
        user.setSex(Sex.secret.type);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        int result = usersMapper.insert(user);

        if (result == 0) {
            return ServerResponse.createByErrorMessage("注册失败");
        }

        user.setPassword(StringUtils.EMPTY);

        return ServerResponse.createBySuccess("注册成功", user);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ServerResponse<Users> login(String username, String password) {

        int resultCount = usersMapper.checkUsername(username);
        if (resultCount==0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        String md5Password = MD5Util.MD5EncodeUtf8(password);

        Users user = usersMapper.selectLogin(username,md5Password);

        if (user == null) {
            return ServerResponse.createByErrorMessage("用户名或密码错误");
        }

        user.setPassword(StringUtils.EMPTY);

//        Example example = new Example(Users.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("username", username);
//        criteria.andEqualTo("password", md5Password);
//        Users user = usersMapper.selectOneByExample(example);
//        if (user == null) {
//            return ServerResponse.createByErrorMessage("用户名或密码错误");
//        }

        return ServerResponse.createBySuccess("登录成功", user);
    }
}
